package de.hauck.rtmidlet;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;

import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.utils.MyLogger;
import de.hauck.rtmidlet.views.CanvasCharts;
import de.hauck.rtmidlet.views.CanvasIB;
import de.hauck.rtmidlet.views.CanvasImage;
import de.hauck.rtmidlet.views.iCanvas;

public class DownloadImageThread implements Runnable {
//	private Configuration c;
	private String _url;
//	private RTMidlet _main;
//	private Displayable _disp;
	private Displayable _dble;
//	private int _current;
	private int _index;
private int _type;
	public DownloadImageThread(String url, int type, int index, Displayable dble) {
		_dble = dble;
//		_current=current;
		_url = url;
		_index=index;
		_type=type;
		new Thread(this).start();
	}

	public void run() {
		downloadImage(_url);
	}

	public void downloadImage(String url) {
		Image image = null;
		InputStream s = null;

		try {
			HttpConnection hc = (HttpConnection) Connector.open(url);
			//if (_dble instanceof CanvasImage) {
				((iCanvas) _dble).updateStatus(Configuration.CONN_STATUS_CONNECTING);
			//}
			s = hc.openInputStream();
			long length = hc.getLength();
			Configuration.addToDownloadCounter(length);
			//if (_dble instanceof CanvasImage) {
				((iCanvas) _dble).updateStatus(Configuration.CONN_STATUS_CONNECTED);
			//}
//			int i_length = (int) length;
//			System.out.println(i_length);
//			byte[] read_array = new byte[i_length];
//			long parts = length / 10;
//			long counter = 0;
//			_wform.setStatus("Downloading...");
//			while (counter < length) {
//				s.read(read_array, (int) counter, (int) Math.min(parts, length
//						- counter));
//				counter += parts;
//				_wform.setProgress(100 * counter / length, counter + " B / "
//						+ length + " B");
//			}
			image = Image.createImage(s); // throws IoException
			
			s.close();
			hc.close();
			//if (_dble instanceof CanvasImage) {
				((iCanvas) _dble).updateStatus(Configuration.CONN_STATUS_OFFLINE);
			//}
			//image = Image.createImage(read_array, 0, i_length - 1);
		} catch (Exception e) {
			e.printStackTrace();
			String msg = e.getMessage();
			MyLogger.fatal("Error downloading image: "+msg);
			RTMidlet.getInstance().setCurrent(_dble);
			RTMidlet.getInstance().alert("Error", msg, Configuration.ALERT_WAIT_TIME);
			try {
				image = Image.createImage("/notfound.jpg");
			} catch (IOException ex) {
				ex.printStackTrace();
				//_main.alert("Error", ex.getMessage(), c.alertWaitTime);
			}
		}
		if (_dble instanceof CanvasCharts) {
			((CanvasCharts)_dble).showPic(image,_type,_index);
		} else if (_dble instanceof CanvasImage) {
			((iCanvas)_dble).showPic(image,_index);
		} else if (_dble instanceof CanvasIB) {
			((CanvasIB)_dble).showPic(image);
		} 
		else System.out.println("image downloaded, don't know what to do");
	}
}
